package com.swx.identityservice.webapis;


import com.swx.identityservice.dtos.permission.InsertPermissionDto;
import com.swx.identityservice.dtos.permission.PermissionRoleDto;
import com.swx.identityservice.dtos.permission.PermissionUserDto;
import com.swx.identityservice.dtos.permission.UpdatePermissionDto;
import com.swx.identityservice.models.ScbpPermission;
import com.swx.identityservice.models.ScbpRolePermission;
import com.swx.identityservice.models.ScbpUserPermission;
import com.swx.identityservice.service.ScbpPermissionService;
import com.swx.identityservice.service.ScbpRolePermissionService;
import com.swx.identityservice.service.ScbpUserPermissionService;
import com.swx.identityservice.utils.JaxrsResponse;
import com.swx.identityservice.utils.ResponseMessages;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lxh
 * 权限 controller.
 * @version V1.0.0
 * @date 2018/6/3 0003 0:32
 */
@Slf4j
@Api(tags = "权限控制器")
@RestController
public class ScbpPermissionController {

    @Autowired
    private ScbpPermissionService scbpPermissionService;
    private ModelMapper modelMapper;
    @Autowired
    private ScbpUserPermissionService scbpUserPermissionService;
    @Autowired
    private ScbpRolePermissionService scbpRolePermissionService;

    public ScbpPermissionController() {
        modelMapper = new ModelMapper();
    }

    @ApiOperation("新增权限")
    @PostMapping(value = "/addpermission")
    public JaxrsResponse post(@RequestBody InsertPermissionDto insertPermissionDto) {
        ScbpPermission scbpPermission = modelMapper.map(insertPermissionDto, ScbpPermission.class);
        scbpPermission.setCreationtime(new Date());
        int sr = scbpPermissionService.insert(scbpPermission);
        if (sr > 0) return JaxrsResponse.builder().code(0).data(insertPermissionDto).status("success").build();
        return JaxrsResponse.builder().code(1).status("fail").data("添加权限失败").message(ResponseMessages.INSERT_FAIL).build();
    }

    @ApiOperation("编辑权限")
    @PostMapping(value = "/editpermission")
    public JaxrsResponse post(@RequestBody UpdatePermissionDto updatePermissionDto) {
        ScbpPermission scbpPermission = modelMapper.map(updatePermissionDto, ScbpPermission.class);
        scbpPermission.setLastmodifytime(new Date());
        int sr = scbpPermissionService.updateByPrimaryKeySelective(scbpPermission);
        if (sr > 0) return JaxrsResponse.builder().code(0).status("success").build();
        return JaxrsResponse.builder().code(1).status("fail").data("修改权限失败").message(ResponseMessages.UPDATE_FAIL).build();
    }


    @ApiOperation("查询授权树结构")
    @GetMapping(value = "/getpermissions")
    public JaxrsResponse getDeptsAsTree() throws Exception {
        List<ScbpPermission> list = scbpPermissionService.getPermissionsAsTree(0);
        return JaxrsResponse.builder().code(0).status("success").data(list).build();
    }

    @ApiOperation("给用户授权")
    @PostMapping(value = "/userpermission")
    public JaxrsResponse userpermission(@RequestBody PermissionUserDto permissionUserDto) {
        int i1 = 0;
        int i2 = scbpUserPermissionService.selectCountByUserId(permissionUserDto.getUserid());
        if (i2 > 0) {
            int i = scbpUserPermissionService.deletePermissionByUserId(permissionUserDto.getUserid());
            if (i <= 0)  return JaxrsResponse.builder().code(1).message("fail").build();
        }
        Integer[] permissions = permissionUserDto.getPermissions();
        for (int i = 0; i < permissions.length; i++) {
            ScbpUserPermission sup = new ScbpUserPermission();
            sup.setUserid(permissionUserDto.getUserid());
            sup.setPermissionid(permissions[i]);
            i1 = scbpUserPermissionService.insertSelective(sup);
        }
        if (i1 > 0)return JaxrsResponse.builder().code(0).status("success").build();
        return JaxrsResponse.builder().code(1).message("fail").build();
    }


    @ApiOperation("给角色授权")
    @PostMapping(value = "/rolepermission")
    public JaxrsResponse rolepermission(@RequestBody PermissionRoleDto roleDto) {
        int i1 = 0;
        int i2 = scbpRolePermissionService.selectCountByRoleId(roleDto.getRoleid());
        if (i2 > 0) {
            int i = scbpRolePermissionService.deletePermissionByRoleId((roleDto.getRoleid()));
            if (i <= 0)  return JaxrsResponse.builder().code(1).message("fail").build();
        }
        Integer[] permissions = roleDto.getPermissions();
        for (int i = 0; i < permissions.length; i++) {
            ScbpRolePermission srp = new ScbpRolePermission();
            srp.setRoleid(roleDto.getRoleid());
            srp.setPermissionid(permissions[i]);
            i1 = scbpRolePermissionService.insertSelective(srp);
        }
        if (i1 > 0) return JaxrsResponse.builder().code(0).status("success").build();
        return JaxrsResponse.builder().code(1).message("fail").build();
    }


    @ApiOperation("查看或者编辑角色授权")
    @PreAuthorize("hasAuthority('Roles_Edit')")
    @GetMapping(value = "/getpermissionbyroleid")
    public JaxrsResponse getpermissionbyroleid(Integer roleId) {
        List<Integer> list = new ArrayList<>();
        List<ScbpRolePermission> lists = scbpRolePermissionService.getpermissionbyroleid(roleId);
        for (int i = 0; i < lists.size(); i++) {
            list.add(lists.get(i).getPermissionid());
        }
        return JaxrsResponse.builder().code(0).status("success").data(list).build();
    }

    @ApiOperation("查看或者编辑用户授权")
    @GetMapping(value = "/getpermissionbyuserid")
    public JaxrsResponse getpermissionbyuserid(Integer userId) {
        List<Integer> list = new ArrayList<>();
        List<ScbpUserPermission> lists = scbpUserPermissionService.getpermissionbyuserid(userId);
        for (int i = 0; i < lists.size(); i++) {
            list.add(lists.get(i).getPermissionid());
        }
        return JaxrsResponse.builder().code(0).status("success").data(list).build();
    }
}
